package com.service.infoEntry.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.infoEntry.dto.CommentDto;
import com.service.infoEntry.dto.ProgrammeCommentDto;
import com.service.infoEntry.dto.UserDto;
import com.service.infoEntry.entity.CommentInfo;
import com.service.infoEntry.entity.ProgrammeCommentInfo;
import com.service.infoEntry.service.CommentService;
import com.service.infoEntry.service.ProgrammeCommentService;
import com.service.infoEntry.vo.ProgrammeCommentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.infoEntry.constant.Constant.*;

@RestController
@CrossOrigin
@Api("项目评论接口")
@RequestMapping("/programmeComment")
public class ProgrammeCommentController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ProgrammeCommentService programmeCommentService;

    @ApiOperation(value = "添加评论")
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ResponseBody
    public Result addComment(@RequestBody ProgrammeCommentDto programmeCommentDto,
                              @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                programmeCommentDto.setUserId(userId);
                programmeCommentService.addComment(programmeCommentDto);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询项目下所有评论")
    @RequestMapping(value = "/queryAllCommentByProgrammeId",method = RequestMethod.GET)
    @ResponseBody
    public Result queryAllCommentByProgramme(@RequestParam("programmeId") String programmeId,
                             @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                List<ProgrammeCommentInfoVo> comments = programmeCommentService.queryAllCommentByProgrammeId(programmeId,userId);
                return Result.ok().setData(comments);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "删除项目下评论")
    @RequestMapping(value = "/deleteCommentByCommentId",method = RequestMethod.DELETE)
    @ResponseBody
    public Result deleteCommentByCommentId(@RequestParam("commentId") String commentId,
                                             @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                programmeCommentService.deleteCommentByCommentId(commentId);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
